package com.top4create.webapp.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.top4create.service.CompanyManager;
import com.top4create.service.GenericManager;
import com.top4create.webapp.util.PageListUtil;
import com.top4create.model.Company;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/companies*")
public class CompanyController {
	private static final int PAGE_SIZE = 2;
	@SuppressWarnings("restriction")
	@Resource(name="companyManager")
	private CompanyManager companyManager;

	// @Autowired
	// public void setCompanyManager(@Qualifier("companyManager") CompanyManager
	// companyManager) {
	// this.companyManager = companyManager;
	// }

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView handleRequest(
			@RequestParam(required = false, value = "q") String query,
			@RequestParam(required = false, value = "page") String page,
			HttpServletRequest request) throws Exception {

		int pageNumber;
		if (page != null
				&& !"".equals(page)) {
			pageNumber = Integer.parseInt(request.getParameter("page"));
		} else {
			pageNumber = 1;
		}
		PageListUtil pageResults = companyManager.getCompanys(pageNumber,
				PAGE_SIZE, null);

		return new ModelAndView().addObject("companyList",pageResults).addObject("size", pageResults.getFullListSize());
	}
}
